<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>websocket</title>
</head>
<body>
{#    <script src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>#}
    <script>
        var ws;
        var wsUrl = 'ws://' + window.location.host + "/net_speed/";

        function WebS() {
            // 判断是否支持ws
            if (!window.WebSocket) {
                alert('该浏览器不支持WebSocket协议,请更换Chrome浏览器访问！');
            } else {
                if (window.s) {
                    window.s.onclose()
                }
                // 建立连接
                ws = new WebSocket(wsUrl);
                ws.onopen = function (evt) {
                    console.log('链接成功！');
                    window.s = ws;
                };
                ws.onclose = function (evt) {
                    console.log('Close Web')
                };
                ws.onmessage = function (evt) {
                    console.log(evt.data)
                };
                ws.onerror = function (evt) {
                    console.log('error')
                };
            }
        }
        window.addEventListener('load', WebS, false);

{#        var socket;#}
{#        var $ = function () {#}
{#            return document.getElementById(arguments[0]);#}
{#        }#}
{#        var log = function (msg) {#}
{#            $("log").innerHTML ? $("log").innerHTML += "<br />" + msg : $("log").innerHTML = msg;#}
{#        }#}
{#        var initWebSocket = function () {#}
{#            if (window.WebSocket) {#}
{#                socket = new WebSocket("ws://127.0.0.1:8080/websocket");#}
{#                socket.onmessage = function (event) {#}
{#                    var json = JSON.parse(event.data);#}
{#                    document.getElementById("message").value = json.message;#}
{#                };#}
{#                socket.onopen = function (event) {#}
{#                    log("Web Socket opened!");#}
{#                };#}
{#                socket.onclose = function (event) {#}
{#                    log("Web Socket closed.");#}
{#                };#}
{#                socket.onerror = function (event) {#}
{#                    log("Web Socket error.");#}
{#                };#}
{#            } else {#}
{#                log("Your browser does not support Web Socket.");#}
{#            }#}
{#        }#}
{#        var send = function () {#}
{#            var data = function () {#}
{#                socket.send("{'send':1,'take':2,'message':" + Math.random() + "}");#}
{#            };#}
{#            if (socket.readyState !== 1) {#}
{#                socket.close();#}
{#                initWebSocket();#}
{#                setTimeout(function () {#}
{#                    data();#}
{#                }, 250);#}
{#            } else {#}
{#                data();#}
{#            }#}
{#        }#}
{#        window.onload = function () {#}
{#            initWebSocket();#}
{#        }#}


    </script>
</body>
</html>